﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Practices.Unity;
using Skynet.BusinessEntities;
namespace Skynet.BusinessLogic.DataAccess
{
    public class KindRepository : MasterRepository<Kind>, IKindRepository
    {
        public KindRepository(IUnityContainer container) : base(container) { }
        public void Add(Kind item)
        {
            
        }
        void IKindRepository.Add(Kind item)
        {
            var ctx = this.ContainerService.Resolve<SkyNetLibraryEntities>();
            ctx.Kinds.AddObject(item);
            ctx.SaveChanges();
            item.AcceptChanges();
            ctx.Dispose();
        }

        void IKindRepository.Add(IEnumerable<Kind> items)
        {
            var ctx = this.ContainerService.Resolve<SkyNetLibraryEntities>();
            foreach (var item in items)
            {
                ctx.Kinds.AddObject(item);

            }
            ctx.SaveChanges();
            foreach (var item in items)
            {
                item.AcceptChanges();
            }            
        }

        void IKindRepository.Update(Kind item)
        {
            var ctx = this.ContainerService.Resolve<SkyNetLibraryEntities>();
            var original = (from c in ctx.Kinds where c.Id == item.Id select c).FirstOrDefault();
            if (original == null) throw new System.Data.DeletedRowInaccessibleException("This record have been deleted by another user.");
            ctx.Kinds.ApplyCurrentValues(item);
            ctx.SaveChanges();
            item.AcceptChanges();
        }

        void IKindRepository.Update(IEnumerable<Kind> items)
        {
            var ctx = this.ContainerService.Resolve<SkyNetLibraryEntities>();
            foreach (var item in items)
            {                
                var original = (from c in ctx.Kinds where c.Id == item.Id select c).FirstOrDefault();
                if (original != null) 
                    ctx.Kinds.ApplyCurrentValues(item);   
            }
            ctx.SaveChanges();
            foreach (var item in items)
            {
                item.AcceptChanges();
            }            
        }

        void IKindRepository.Delete(Kind item)
        {
            var ctx = this.ContainerService.Resolve<SkyNetLibraryEntities>();
            var query = (from c in ctx.Kinds where c.Id == item.Id select c).SingleOrDefault();
            if (query != null)
            {
                ctx.Kinds.DeleteObject(query);
                ctx.SaveChanges();
                item.AcceptChanges();
            }
        }

        void IKindRepository.Delete(IEnumerable<Kind> items)
        {
            var ctx = this.ContainerService.Resolve<SkyNetLibraryEntities>();
            foreach (var item in items)
            {
                var original = (from c in ctx.Kinds where c.Id == item.Id select c).FirstOrDefault();
                if (original != null)
                    ctx.Kinds.DeleteObject(item);
            }
            ctx.SaveChanges();
            foreach (var item in items)
            {
                item.AcceptChanges();
            }            
        }

        Kind IKindRepository.GetOne(object key)
        {
            var ctx = this.ContainerService.Resolve<SkyNetLibraryEntities>();
            var query = (from c in ctx.Kinds where c.Id == (Guid)key select c).SingleOrDefault();
            return query;   
        }

        IEnumerable<Kind> IKindRepository.GetAll()
        {
            var ctx = this.ContainerService.Resolve<SkyNetLibraryEntities>();
            var query = (from c in ctx.Kinds select c);
            return query.ToList();
        }


        public Kind GetByTitle(string Title)
        {
            var ctx = this.ContainerService.Resolve<SkyNetLibraryEntities>();
            var query = (from c in ctx.Kinds  where c.Title == Title select c).SingleOrDefault();
            return query; 
        }
    }
}
